package com.hjp.dao.impl;


import java.util.List;

import org.springframework.stereotype.Repository;

import com.hjp.dao.impl.BaseDaoImpl;
import com.hjp.dao.IDepartmentDao;
import com.hjp.model.Department;

@Repository("departmentDaoImpl")
@SuppressWarnings("unchecked")
public class DepartmentDaoImpl extends BaseDaoImpl<Department> implements IDepartmentDao {

	@Override
	public List<Department> findTopList() {
		return sessionFactory.getCurrentSession().createQuery(//
				"FROM Department d WHERE d.parent IS NULL")//
				.list();
	}

	@Override
	public List<Department> findChildren(Long parentId) {
		return sessionFactory.getCurrentSession().createQuery(//
				"FROM Department d WHERE d.parent.id=?")//
				.setParameter(0, parentId)//
				.list();
	}

	/* (non-Javadoc)
	 * @see cn.itcast.oa.dao.IDepartmentDao#findByName(java.lang.String)
	 */
	@Override
	public Department getByName(String name) {
		// TODO Auto-generated method stub
		List<Department> list = sessionFactory.getCurrentSession()
				.createQuery("FROM Department d WHERE d.name=?")
				.setParameter(0, name).list();
		if (!list.isEmpty()) {

			return (Department) list.get(0);
		}
		else{
			return null;
		}
	}
	
}
